package com.huage.courseplatform.framework.utils;

import com.huage.courseplatform.framework.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


/**
 * 统一返回格式
 * @param <T> 返回的数据类型
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value="响应参数",description="响应参数")
public class ResultUtils<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态码")
    private String code;
    @ApiModelProperty(value = "消息")
    private String msg;
    @ApiModelProperty(value = "响应内容")
    private T data;

    public ResultUtils(T data) {
        this.data = data;
    }

    public static ResultUtils success() {
        ResultUtils resultUtils = new ResultUtils<>();
        resultUtils.setCode(ResultCode.SUCCESS.getCode());
        resultUtils.setMsg(ResultCode.SUCCESS.getMsg());
        return resultUtils;
    }

    public static <T> ResultUtils<T> success(T data) {
        ResultUtils<T> resultUtils = new ResultUtils<>(data);
        resultUtils.setCode(ResultCode.SUCCESS.getCode());
        resultUtils.setMsg(ResultCode.SUCCESS.getMsg());
        resultUtils.setData(data);
        return resultUtils;
    }

    public static <T> ResultUtils<T> success(String msg, T data) {
        ResultUtils<T> resultUtils = new ResultUtils<>(data);
        resultUtils.setCode(ResultCode.SUCCESS.getCode());
        resultUtils.setMsg(msg);
        resultUtils.setData(data);
        return resultUtils;
    }

    public static ResultUtils error(String code, String msg) {
        ResultUtils resultUtils = new ResultUtils<>();
        resultUtils.setCode(code);
        resultUtils.setMsg(msg);
        return resultUtils;
    }

    public static ResultUtils error() {
        ResultUtils resultUtils = new ResultUtils<>();
        resultUtils.setCode(ResultCode.ERROR.getCode());
        resultUtils.setMsg(ResultCode.ERROR.getMsg());
        return resultUtils;
    }

    public static ResultUtils error(String msg) {
        ResultUtils resultUtils = new ResultUtils<>();
        resultUtils.setCode(ResultCode.ERROR.getCode());
        resultUtils.setMsg(msg);
        return resultUtils;
    }


    public static <T> ResultUtils<T> error(String msg, T data) {
        ResultUtils<T> resultUtils = new ResultUtils<>(data);
        resultUtils.setCode(ResultCode.ERROR.getCode());
        resultUtils.setMsg(msg);
        resultUtils.setData(data);
        return resultUtils;
    }
}
